# -*- coding: utf-8 -*-
from decimal import Decimal

class Position(object):

    def __init__(self,ticker,quantity ,avg_price,bid,ask,commission = 0):

        self.ticker = ticker
        self.quantity  = quantity
        self.avg_price = avg_price
        self.total_commission = commission

        #初始化其他值
        self.realised_pnl = 0
        self.unrealised_pnl = 0
        self.update_market_value(bid, ask)

    def update_market_value(self,bid,ask):

        midpoint = (bid + ask) // 2
        self.market_value = self.quantity * midpoint
        #计算为实现利润
        self.unrealised_pnl = self.market_value - self.avg_price * self.quantity - self.total_commission

    def transact_shares(self, action,quantity, price, commission):
        if action == "BOT":
            self.total_commission += commission
            self.avg_price = (self.avg_price * self.quantity + price * quantity + commission) // (self.quantity+quantity)
            self.quantity += quantity
        #action = SLD
        else:
            self.realised_pnl += quantity * (price - self.avg_price) - commission
            self.quantity -= quantity




